环信app的推送功能实现 |
您所在的位置:网站首页 › uni app 模板 › 环信app的推送功能实现 |
今天 花了一天的时间终于将推送给弄好了,才发现其实很简单,就是因为自己的几个方法木有实现才导致花了那么多的冤枉时间 推送的步骤如下 1.先创建一个apns推送证书,请参照:http://developer.easemob.com/docs/emchat/ios/push/certificate.html 创建证书后将apns证书打包成p12文件,然后上传到环信的后台 2.在创建开发者证书和描述文件,这个证书跟上面的apns证书是不同的,这个证书是用来真机调试的,appid要有推送功能 3.将环信的appkey和证书写入工程中,代码如下 //注册 APNS文件的名字, 需要与后台上传证书时的名字一一对应 NSString *apnsCertName = @"125642"; [[EaseMob sharedInstance] registerSDKWithAppKey:@“4dasfdas” apnsCertName:apnsCertName]; [[EaseMob sharedInstance] enableBackgroundReceiveMessage]; [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; 4.实现收到消息的几个回调方法就可以了 // 收到消息回调 -(void)didReceiveMessage:(EMMessage *)message{
#if !TARGET_IPHONE_SIMULATOR [self playSoundAndVibration];
BOOL isAppActivity = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive; if (!isAppActivity) { [self showNotificationWithMessage:message]; } #endif }
- (void)playSoundAndVibration{
//如果距离上次响铃和震动时间太短, 则跳过响铃 NSLog(@"%@, %@", [NSDate date], self.lastPlaySoundDate); NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate]; if (timeInterval < kDefaultPlaySoundInterval) { return; } //保存最后一次响铃时间 self.lastPlaySoundDate = [NSDate date];
// 收到消息时,播放音频 [[EaseMob sharedInstance].deviceManager asyncPlayNewMessageSound]; // 收到消息时,震动 [[EaseMob sharedInstance].deviceManager asyncPlayVibration]; }
- (void)showNotificationWithMessage:(EMMessage *)message { EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions]; //发送本地推送 UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate date]; //触发通知的时间
if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) { id messageBody = [message.messageBodies firstObject]; NSString *messageStr = nil; switch (messageBody.messageBodyType) { case eMessageBodyType_Text: { messageStr = ((EMTextMessageBody *)messageBody).text; } break; case eMessageBodyType_Image: { messageStr = @"[图片]"; } break; case eMessageBodyType_Location: { messageStr = @"[位置]"; } break; case eMessageBodyType_Voice: { messageStr = @"[音频]"; } break; case eMessageBodyType_Video:{ messageStr = @"[视频]"; } break; default: break; }
NSString *title = message.from; if (message.isGroup) { NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList]; for (EMGroup *group in groupArray) { if ([group.groupId isEqualToString:message.conversation.chatter]) { title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, group.groupSubject]; break; } } }
notification.alertBody = [NSString stringWithFormat:@"%@:%@", title, messageStr]; } else{ notification.alertBody = @"您有一条新消息"; }
#warning 去掉注释会显示[本地]开头, 方便在开发中区分是否为本地推送 //notification.alertBody = [[NSString alloc] initWithFormat:@"[本地]%@", notification.alertBody];
notification.alertAction = @"打开"; notification.timeZone = [NSTimeZone defaultTimeZone]; //发送通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; UIApplication *application = [UIApplication sharedApplication]; application.applicationIconBadgeNumber += 1; }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |